package com.cm.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cm.common.entity.PageParam;
import com.cm.common.utils.COSStorage;
import com.cm.common.utils.StringUtil;
import com.cm.goods.entity.Brand;
import com.cm.goods.entity.dto.BrandKeyWordDTO;
import com.cm.goods.entity.vo.BrandVO;
import com.cm.goods.mapper.BrandMapper;
import com.cm.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @类 名:BrandServiceImpl </br>
 * @描 述:<br>
 * @日 期:2020/9/23 17:07 </br>
 * @作 者:小兰同学</br>
 * @版 本：1.0.0
 * @since JDK 1.8
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public boolean insert(Brand brand) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",brand.getName());
        List<Brand> brands = brandMapper.selectList(queryWrapper);
        if(brands.size() > 0){
            return false;
        }
        brandMapper.insert(brand);
        return true;
    }

    @Override
    public boolean delete(Long id) {
        String url=brandMapper.selectById(id).getLogo();
        String key= StringUtil.getKey(url);
        if(brandMapper.deleteById(id) <0){
            return false;
        }
        COSStorage.delete(key);
        return true;
    }

    @Override
    public boolean update(Brand brand) {
        Long id =brand.getId();
        String url=brandMapper.selectById(id).getLogo();
        String key= StringUtil.getKey(url);
        COSStorage.delete(key);
        if (brandMapper.updateById(brand) > 0){
            return true;
        }
        return false;
    }

    @Override
    public IPage<BrandVO> getBrandList(BrandKeyWordDTO brandKeyWordDTO) {
        Page<BrandVO> page = new Page<>();
        brandKeyWordDTO.setCurrentPage((brandKeyWordDTO.getCurrentPage()-1)*brandKeyWordDTO.getPageSize());
        List<BrandVO> brandList=brandMapper.getBrandList(brandKeyWordDTO);
        page.setRecords(brandList);
        page.setCurrent(brandKeyWordDTO.getCurrentPage());
        page.setSize(brandKeyWordDTO.getPageSize());
        int count=brandMapper.getBrandTotal(brandKeyWordDTO);
        page.setTotal(count);
        return page;
    }
}
